<?php
class Controller_User extends FLEA_Controller_Action {
	function actionindex(){
		$table_user = & FLEA::getSingleton('Model_users');
		$user = $table_user->findAll();
		$smarty =& $this->_getView();
		$smarty->assign('user', $user);
		$smarty->display('userlist.html');
		
	}

	function actionuserlist() {
		$table_user = & FLEA::getSingleton('Model_users');
		$user = $table_user->findAll();
		// $user = $table_user->findBySql('show columns from users');
        // exit;
		$smarty =& $this->_getView();
		$smarty->assign('user', $user);
		$smarty->display('userlist.html');
	}
	function actionadduser() {
        if ($this->_isPOST()){
			$row = array(
				'username' => strip_tags($_POST['username']),
				'password' => strip_tags($_POST['password']),
                'password_conform' => strip_tags($_POST['password_conform']),
				'email' => strip_tags($_POST['email']),
				'sex' => (int)$_POST['sex'],
				);
            if(empty($row['username']) || empty($row['password'])){
                js_alert('用户名密码不能为空', 'history.back();');
            }
            elseif($row['password_conform'] != $row['password']){
                js_alert('两次输入密码不一致', 'history.back();');
            }
            $table_user = & FLEA::getSingleton('Model_users');
            $row[$table_user->rolesField][] = 3;
            if($table_user->save($row)){
                js_alert('更新成功','history.back();');
            }
            else{
                js_alert('更新失败，请重试或联系管理员','history.back();');
            }
        }
        else{
            $table_roles = FLEA::getSingleton('Model_roles');
            $roles = $table_roles->findAll();
            $smarty =& $this->_getView();
            $smarty->assign('roles', $roles);
            $smarty->display('user.html');
        }
	}
	function actionedituser() {
            $userid = (int)$_GET['user_id'];
            $table_user = & FLEA::getSingleton('Model_users');
		    $user = $table_user->find($userid);
            $table_dep1 = FLEA::getSingleton('Table_dep1');
		    $dep1[] = $table_dep1->find($row['dep1']);
            $dep2 = $dep1[0]['dep2'];
            $table_roles = FLEA::getSingleton('Model_roles');
		    $roles = $table_roles->findAll();

            if(false == empty($user['roles'])){
                foreach($roles as $key => &$value) {
                    foreach($user['roles'] as $k => $v) {
                        if($v['role_id'] == $value['role_id']) {
                            $value['selected'] = true;                   
                        }              
                    }         
                }
            }
		    $smarty =& $this->_getView();
            $smarty->assign('form', $user);
		    $smarty->assign('dep1', $dep1);
		    $smarty->assign('dep2', $dep2);
            $smarty->assign('user_id', $userid);
            $smarty->assign('roles', $roles);
		    $smarty->display('admin/user.html');
    }
	function actiondeluser(){
		$table_user = FLEA::getSingleton('Model_users');
		$table_user->removeByPkv((int)$_GET['user_id']);
		redirect($this->_url());
    }
}
